//
//  LoginViewModel.m
//  zhuchiren
//
//  Created by rainday on 15/1/28.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "LoginViewModel.h"
#import "IP.h"

#import <AFNetworking.h>
#import <RACEXTScope.h>
#import <ReactiveCocoa.h>

@implementation LoginViewModel

- (id)init{
    self = [super init];
    if (self) {
        _isLogin = NO;
        
    }
    return self;

}

+ (RACSignal *)loginWithUsername:(NSString *)username PassWord:(NSString *)password{
    
    
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        
        
        NSString *url = [NSString stringWithFormat:@"%s/token",SERVER];
        
        
        NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:username,@"phone",password,@"password", nil];
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        

        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        
        [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            
            [userDefaults setObject:[responseObject objectForKey:@"token"] forKey:@"token"];
            [userDefaults setObject:[responseObject objectForKey:@"username"] forKey:@"username"];
            [userDefaults setObject:[responseObject objectForKey:@"nickname"] forKey:@"nickname"];
            [userDefaults setObject:[responseObject objectForKey:@"phone"] forKey:@"phone"];
            [userDefaults setObject:[responseObject objectForKey:@"gender"] forKey:@"gender"];
            [userDefaults setObject:[responseObject objectForKey:@"height"] forKey:@"height"];
            [userDefaults setObject:[responseObject objectForKey:@"avatar"] forKey:@"avatar"];
            [userDefaults setObject:[responseObject objectForKey:@"sign"] forKey:@"sign"];
            
            [userDefaults synchronize];
            [subscriber sendNext:responseObject];
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            [subscriber sendError:error];
            
        }];
        
        
        return nil;
    }];
}


+ (RACSignal *)logout{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSString *url = [NSString stringWithFormat:@"%s/token",SERVER];

        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        id token = [userDefaults objectForKey:@"token"];
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        
        [manager DELETE:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            [subscriber sendError:error];
            
        }];
    
        
        return nil;
    }];
}

+ (RACSignal *)registerPhone:(NSString *)phone nickname:(NSString *)nickname birthday:(NSString *)birthday height:(NSString *)height password:(NSString *)password gender:(NSString *)gender{

    
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        
        
        NSString *url = [NSString stringWithFormat:@"%s/user",SERVER];
        
        
        NSDictionary *parameters = @{@"password":password,
                                     @"nickname":nickname,
                                     @"birthday":birthday,
                                     @"height":height,
                                     @"gender":gender,
                                     @"phone":phone,
                                     @"age":@24};
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        
        [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            
            [userDefaults setObject:[responseObject objectForKey:@"token"] forKey:@"token"];
            [userDefaults setObject:[responseObject objectForKey:@"username"] forKey:@"username"];
            [userDefaults setObject:[responseObject objectForKey:@"nickname"] forKey:@"nickname"];
            [userDefaults setObject:[responseObject objectForKey:@"phone"] forKey:@"phone"];
            [userDefaults setObject:[responseObject objectForKey:@"gender"] forKey:@"gender"];
            [userDefaults setObject:[responseObject objectForKey:@"height"] forKey:@"height"];
            [userDefaults setObject:[responseObject objectForKey:@"avatar"] forKey:@"avatar"];
            [userDefaults setObject:[responseObject objectForKey:@"sign"] forKey:@"sign"];
            [userDefaults synchronize];
            [subscriber sendNext:responseObject];
            [subscriber sendNext:responseObject];
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            [subscriber sendError:error];
            
        }];
        
        
        return nil;
    }];
}
@end
